package com.group5.onlinegame.handler;


import com.github.pagehelper.PageInfo;
import com.group5.onlinegame.entity.Game;
import com.group5.onlinegame.entity.GameRoom;
import com.group5.onlinegame.entity.GameType;
import com.group5.onlinegame.service.IGameService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/")
public class GameHandler {
    @Autowired
    private IGameService GameService;

    @RequestMapping("game")
    @RequiresRoles("game")
    public ModelAndView getList(@RequestParam(required =true,defaultValue = "1",value = "pageNum")Integer page){
        ModelAndView mv =new ModelAndView("game");
        PageInfo<Game> pageInfo =GameService.getList(page);
        List<GameType> list=GameService.getType();
        System.out.println(pageInfo.getList());
        mv.addObject("typelist",list);
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    @RequestMapping("gametype")
    @RequiresRoles("game")
    public ModelAndView getTypeList(@RequestParam(required = false,defaultValue = "1",value = "typePageNum")Integer page){
        ModelAndView mv =new ModelAndView("gametype");
        PageInfo<GameType> gameTypePageInfo=GameService.getTypeList(page);
        mv.addObject("typePageInfo",gameTypePageInfo);
        return mv;
    }
    @RequestMapping("form")
    @ResponseBody
    @RequiresRoles("game")
    public Game editGame(@RequestParam(required = true,name = "id")Integer id){
        Game game=GameService.getGameById(id);
        return game;
    }

    @RequestMapping("addGame")
    @RequiresRoles("game")
    public String addGame(Game game,@RequestParam(name = "typeid",required = true)Integer typeid){
        GameType gameType=new GameType();
        gameType.setTypeid(typeid);
        game.setGameType(gameType);
        GameService.addGame(game);
        return "redirect:/game";
    }
    @RequestMapping("editGame")
    @RequiresRoles("game")
    public String editGameById(Game game,@RequestParam(name = "typeid",required = true)Integer typeid){
        GameType gameType=new GameType();
        gameType.setTypeid(typeid);
        game.setGameType(gameType);
        GameService.editGame(game);
        System.out.println(game);
        return "redirect:/game";
    }
    @RequestMapping("delGame")
    @RequiresRoles("game")
    public String delGame(Integer id){
        GameService.delGame(id);
        return "redirect:/game";
    }

    @RequestMapping("batchDeletes")
    @RequiresRoles("game")
    public ModelAndView batchDeletes(String tag){
        String[] strs = tag.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                GameService.delGame(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return new ModelAndView("redirect:/game");
    }
    @RequestMapping("gametypeDeletes")
    @RequiresRoles("game")
    public ModelAndView gametypeDeletes(String tag){
        String[] strs = tag.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                GameService.delType(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return new ModelAndView("redirect:/gametype");
    }
    @RequestMapping("roomDeletes")
    @RequiresRoles("game")
    public ModelAndView roomDeletes(String tag){
        String[] strs = tag.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                GameService.delRoom(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return new ModelAndView("redirect:/room");
    }

    @RequestMapping("editgametype")
    @ResponseBody
    @RequiresRoles("game")
    public GameType gameType(@RequestParam(required = true,name = "typeid")Integer typeid){
        GameType gameType=GameService.gameType(typeid);
        return gameType;
    }
    @RequestMapping("changegametype")
    @RequiresRoles("game")
    public String changegametype(GameType gameType){
        GameService.editGameType(gameType);
        return "redirect:/gametype";
    }
    @RequestMapping("addGameType")
    @RequiresRoles("game")
    public String addGameType(GameType gameType){
        GameService.addGameType(gameType);
        return "redirect:/gametype";
    }
    @RequestMapping("deltype")
    @RequiresRoles("game")
    public String delType(Integer typeid){
        GameService.delType(typeid);
        return "redirect:/gametype";
    }

    @RequestMapping("room")
    @RequiresRoles("game")
    public ModelAndView getRoomList(@RequestParam(required = false,defaultValue = "1",value = "roomPageNum")Integer page){
        ModelAndView mv =new ModelAndView("gameroom");
        PageInfo<GameRoom> getRoomList=GameService.getRoomList(page);
        mv.addObject("roomPageInfo",getRoomList);
        return mv;
    }
    @RequestMapping("checkroom")
    @ResponseBody
    @RequiresRoles("game")
    public GameRoom checkRoom(@RequestParam(required = true,name = "roomid")Integer roomid){
        GameRoom gameRoom =GameService.checkRoom(roomid);
        return gameRoom;
    }
    @RequestMapping("editRoom")
    @RequiresRoles("game")
    public String editRoom(GameRoom gameRoom){
        GameService.editRoom(gameRoom);
        return "redirect:/room";
    }
    @RequestMapping("delRoom")
    @RequiresRoles("game")
    public String delRoom(Integer roomid){
        GameService.delRoom(roomid);
        return "redirect:/room";
    }
    @RequestMapping("roomCheck")
    @RequiresRoles("game")
    public ModelAndView roomCheck(String tag){
        String[] strs = tag.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                GameService.roomCheck(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return new ModelAndView("redirect:/room");
    }
}
